package com.test.jpa.helloworld;

import javax.persistence.*;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

/**
 * @author lzy
 * @version v1.0
 * Create DateTime: 2018/10/16 21:19
 */
@NamedQuery(name = "testNamedQuery", query = "SELECT c FROM Customer c WHERE c.id=?")
@Cacheable()
@Table(name = "jpa_customer")
@Entity
public class Customer {

    private Integer id;
    private String lastName;
    private String email;
    private int age;
    private Date createTime;
    private Date birth;

    private Set<Order> orders = new HashSet<>();

    //映射单向1-n的关联关系
    //使用@OneToMany映射1-n的关联关系
    //使用@JoinColumn来映射外键列的名称
    //可以使用 @OneToMany的fetch属性来修改默认的加载策略
    //可以通过修改@OneToMany的cascade属性来修改默认的删除策略
    //注意：若在1的一端的@OneToMany中使用mappedBy属性，则@OneToMany端就不能同时使用@JoinColumn属性
//    @JoinColumn(name = "customer_id")
    @OneToMany(fetch = FetchType.LAZY, cascade = {CascadeType.REMOVE}, mappedBy = "customer")
    public Set<Order> getOrders() {
        return orders;
    }

    public void setOrders(Set<Order> orders) {
        this.orders = orders;
    }

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "customer_id")
//    @GeneratedValue(strategy = GenerationType.TABLE,
//            generator = "ID_GENERATOR")
//    @TableGenerator(name = "ID_GENERATOR",
//            table = "jpa_id_generators",
//            pkColumnName = "PK_NAME",
//            pkColumnValue = "CUSTOMER_ID",
//            valueColumnName = "PK_VALUE",
//            allocationSize = 100)
    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    @Column(name = "last_name", length = 50, nullable = false)
    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    // 工具方法，不需要映射为数据表的一列
    @Transient
    public String getInfo() {
        return "lastName" + lastName + ", email" + email;
    }

    @Temporal(TemporalType.TIMESTAMP)
    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    @Temporal(TemporalType.DATE)
    public Date getBirth() {
        return birth;
    }

    public void setBirth(Date birth) {
        this.birth = birth;
    }

    @Override
    public String toString() {
        return "Customer{" +
                "id=" + id +
                ", lastName='" + lastName + '\'' +
                ", email='" + email + '\'' +
                ", age=" + age +
                ", createTime=" + createTime +
                ", birth=" + birth +
                '}';
    }

    public Customer() {
    }

    public Customer(String lastName, int age) {
        this.lastName = lastName;
        this.age = age;
    }
}
